#include "graphicview.h"
#include "application.h"


GraphicView::GraphicView(Application* app, QWidget *parent) :
        QGraphicsView(parent), app_(app)
{
    grabGesture(Qt::PanGesture);
    grabGesture(Qt::PinchGesture);
    grabGesture(Qt::SwipeGesture);
}

bool GraphicView::event(QEvent *event)
{
    if (event->type() == QEvent::Gesture)
        return gestureEvent(static_cast<QGestureEvent*>(event));
    return QGraphicsView::event(event);
}

void GraphicView::resizeEvent(QResizeEvent *event) {
    if (scene())
        scene()->setSceneRect(QRect(QPoint(0, 0), event->size()));
    app_->updateView();
    QGraphicsView::resizeEvent(event);
}

void GraphicView::paintEvent(QPaintEvent * event)
{
    QGraphicsView::paintEvent(event);
//    QPainter p(this);
//    p.fillRect(event->rect(), QColor(128, 128, 128, 128));
//    p.drawRoundedRect(event->rect(), 5, 5);
//    p.end();
}

void GraphicView::mouseMoveEvent(QMouseEvent * event)
{
    QGraphicsView::mouseMoveEvent(event);
}

void GraphicView::keyPressEvent (QKeyEvent * event)
{
    switch(event->key()){
    case Qt::Key_F1:
        app_->showMenu();
        break;
    }

    QGraphicsView::keyPressEvent(event);
}


void GraphicView::panTriggered(QPanGesture *gesture)
{
#ifndef QT_NO_CURSOR
    switch (gesture->state()) {
    case Qt::GestureStarted:
    case Qt::GestureUpdated:
        setCursor(Qt::SizeAllCursor);
        break;
    default:
        setCursor(Qt::ArrowCursor);
    }
#endif
    QPointF delta = gesture->delta();
    //horizontalOffset += delta.x();
    //verticalOffset += delta.y();
    update();
}

void GraphicView::pinchTriggered(QPinchGesture *gesture)
{
    QPinchGesture::ChangeFlags changeFlags = gesture->changeFlags();
    if (changeFlags & QPinchGesture::RotationAngleChanged) {
        //qreal value = gesture->property("rotationAngle").toReal();
        // qreal lastValue = gesture->property("lastRotationAngle").toReal();
        // rotationAngle += value - lastValue;
    }
    if (changeFlags & QPinchGesture::ScaleFactorChanged) {
        //  qreal value = gesture->property("scaleFactor").toReal();
        //  currentStepScaleFactor = value;
    }
    if (gesture->state() == Qt::GestureFinished) {
        //  scaleFactor *= currentStepScaleFactor;
        //  currentStepScaleFactor = 1;
    }
    //   update();
}

void GraphicView::swipeTriggered(QSwipeGesture *gesture)
{
    if (gesture->state() == Qt::GestureFinished) {
        if (gesture->horizontalDirection() == QSwipeGesture::Left
            || gesture->verticalDirection() == QSwipeGesture::Up)
            app_->showMenu();
        else{}
        //goNextImage();
        // update();
    }
}


bool GraphicView::gestureEvent(QGestureEvent *event)
{
    if (QGesture *swipe = event->gesture(Qt::SwipeGesture))
        swipeTriggered(static_cast<QSwipeGesture *>(swipe));
    else if (QGesture *pan = event->gesture(Qt::PanGesture))
        panTriggered(static_cast<QPanGesture *>(pan));
    if (QGesture *pinch = event->gesture(Qt::PinchGesture))
        pinchTriggered(static_cast<QPinchGesture *>(pinch));
    return true;
}
